package com.example.poi.web.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.message.ObjectMessage;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.poi.entity.Student;
import com.example.poi.entity.TbClass;
import com.example.poi.service.ClassService;
import com.example.poi.service.StudentService;

@Controller
public class HomeController {
	@Autowired
	private StudentService studentService;
	@Autowired
	private ClassService classService;
	
	@RequestMapping(value= {"/index","/"})
	public String index() {
		return "index";
	}
	
	@RequestMapping(value="/export")
	@ResponseBody
	public void export(HttpServletRequest request,HttpServletResponse response) {
		HSSFWorkbook workbook=new HSSFWorkbook();
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setWrapText(true);
		cellStyle.setAlignment(HorizontalAlignment.CENTER);
		cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		cellStyle.setBorderBottom(BorderStyle.THIN);
		cellStyle.setBorderLeft(BorderStyle.THIN);
		cellStyle.setBorderRight(BorderStyle.THIN);
		cellStyle.setBorderTop(BorderStyle.THIN);
		HSSFSheet sheet = workbook.createSheet("学生表");
		String[] fieldnames= {"班级","学号","姓名","性别","密码"};
		HSSFRow row = sheet.createRow(0);
		for(int i=0;i<fieldnames.length;i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellValue(fieldnames[i]);
			cell.setCellStyle(cellStyle);
		}
		int rowCount=1;
		List<TbClass> classes = classService.findAll();
		for(int i=0;i<classes.size();i++) {
			TbClass bj=classes.get(i);
			List<Student> students=studentService.findByTbClassId(bj.getId());
			for(int j=0;j<students.size();j++,rowCount++) {
				Student s = students.get(j);
				row=sheet.createRow(rowCount);
				HSSFCell cell=row.createCell(0);
				cell.setCellStyle(cellStyle);
				cell = row.createCell(1);
				cell.setCellValue(s.getStuNum());
				cell.setCellStyle(cellStyle);
				cell=row.createCell(2);
				cell.setCellValue(s.getName());
				cell.setCellStyle(cellStyle);
				cell=row.createCell(3);
				cell.setCellValue(s.getSex());
				cell.setCellStyle(cellStyle);
				cell=row.createCell(4);
				cell.setCellValue(s.getPassword());
				cell.setCellStyle(cellStyle);
			}
			if(students.size()>0) {
				 CellRangeAddress region = new CellRangeAddress(rowCount-students.size(), rowCount-1, 0, 0);
				 sheet.addMergedRegion(region);
				 HSSFCell cell = sheet.getRow(rowCount-students.size()).getCell(0);
				 cell.setCellValue(bj.getName());
			}
		}
		/*sheet.setColumnWidth(0, 20*256);*/
		String filename="student.xls";
		response.setHeader("Content-disposition", "attachment; filename="+filename);  
		response.setContentType("application/msexcel");
		ServletOutputStream outputStream;
		try {
			outputStream = response.getOutputStream();
			workbook.write(outputStream);
			workbook.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
	
	@RequestMapping(value="/aaa")
	@ResponseBody
	public Object aaa(String username,String password) {
		System.out.println(username);
		System.out.println(password);
		Student student= studentService.findAll().get(0);
		return student;
		
	}
}
